﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Web.Tool
{
    public class ExceptionLog
    {
        public int ExceptionId { get; set; }
        public string TaskID { get; set; }
        public string OperatUserID { get; set; }
        public string OperatName { get; set; }
        public string ExceptionMessage { get; set; }
        public string IDS
        {
            get;
            set;
        }
        public string FailEail
        {
            get;
            set;
        }
        public DateTime DateCreated { get; set; }

        public bool LogToFile()
        {
            //返回的是物理路径，如果还有子目录注意要用\\，比如"AppExcLog\\folderName\\abc"  
            string tagFolder = AppDomain.CurrentDomain.BaseDirectory + "AppExcLog\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";
            string logFile = tagFolder + DateTime.Now.ToString("yyyyMMddHH") + ".txt";
            if (!Directory.Exists(tagFolder))
            {
                Directory.CreateDirectory(tagFolder);
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("TaskID:" + this.TaskID + "\r\n");  
            sb.Append("OperatUserID:" + this.OperatUserID + "\r\n");  
            sb.Append("OperatName:" + this.OperatName + "\r\n");  
            sb.Append("CreateTime:" + this.DateCreated + "\r\n");
            sb.Append("ExceptionMessage:" + this.ExceptionMessage + "\r\n");
            sb.Append("---------------------\r\n\r\n");

            try
            {
                StreamWriter writer = null;
                try
                {
                    writer = new StreamWriter(logFile, true, System.Text.Encoding.UTF8);
                    writer.Write(sb.ToString());
                    return true;
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                        writer = null;
                    }
                }
            }
            catch
            {
                throw;
            }
        }
        /// <summary>  
        /// 重载方法  
        /// </summary>  
        /// <param name="preFix">新文件前缀</param>  
        /// <returns></returns>  
        public bool LogToFile(string preFix)
        {
            //返回的是物理路径，如果还有子目录注意要用\\，比如"AppExcLog\\folderName\\abc"  
            string tagFolder = AppDomain.CurrentDomain.BaseDirectory + "AppExcLog\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";
            string logFile = tagFolder + preFix + "_" + DateTime.Now.ToString("yyyyMMddHH") + ".txt";
            if (!Directory.Exists(tagFolder))
            {
                Directory.CreateDirectory(tagFolder);
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("CreateTime:" + this.DateCreated + "\r\n");
            sb.Append("ExceptionMessage:" + this.ExceptionMessage + "\r\n");
            sb.Append("---------------------\r\n\r\n");

            try
            {
                StreamWriter writer = null;
                try
                {
                    writer = new StreamWriter(logFile, true, System.Text.Encoding.UTF8);
                    writer.Write(sb.ToString());
                    return true;
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                        writer = null;
                    }
                }
            }
            catch
            {
                throw;
            }
        }
        public bool IDsToFile(bool success)
        {
            string tagFolder = string.Empty;
            string logFile = string.Empty;
            if (success)
            {
                tagFolder = AppDomain.CurrentDomain.BaseDirectory + "AppExcLog\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";
                logFile = tagFolder + this.TaskID + '_' + DateTime.Now.ToString("yyyyMMddHH") + "_IDS.txt";
            }
            else
            {
                tagFolder = AppDomain.CurrentDomain.BaseDirectory + "AppExcLog\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\" + "FailIds\\";
                logFile = tagFolder + this.TaskID + '_' + DateTime.Now.ToString("yyyyMMddHH") + "_IDS.txt";
            }
            if (!Directory.Exists(tagFolder))
            {
                Directory.CreateDirectory(tagFolder);
            }
            StringBuilder sb = new StringBuilder();
            sb.Append(this.IDS + ',');

            try
            {
                StreamWriter writer = null;
                try
                {
                    if (!File.Exists(logFile))
                    {
                        CreatTxtFileToDataBase(logFile);
                    }
                    writer = new StreamWriter(logFile, true, System.Text.Encoding.UTF8);
                    writer.Write(sb.ToString());
                    return true;
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                        writer = null;
                    }
                }
            }
            catch
            {
                throw;
            }
        }

        public string FailEmailToFile()
        {
            string tagFolder = string.Empty;
            string logFile = string.Empty;
            tagFolder = AppDomain.CurrentDomain.BaseDirectory + "AppExcLog\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";
            logFile = tagFolder + this.TaskID + '_' + "FailEmail.txt";

            if (!Directory.Exists(tagFolder))
            {
                Directory.CreateDirectory(tagFolder);
            }
            StringBuilder sb = new StringBuilder();
            sb.Append(this.FailEail + ',');

            try
            {
                StreamWriter writer = null;
                try
                {
                    //if (!File.Exists(logFile))  
                    //{  
                    //    CreatTxtFileToDataBase(logFile);  
                    //}  
                    writer = new StreamWriter(logFile, true, System.Text.Encoding.UTF8);
                    writer.Write(sb.ToString());
                    return logFile;
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                        writer = null;
                    }
                }
            }
            catch
            {
                throw;
            }
        }

        void CreatTxtFileToDataBase(object obj)
        {
            string logFile = obj.ToString();
            //BLLMail mail = new BLLMail();
            // mail.CreateTxtFile(int.Parse(this.TaskID), logFile, 10);
            return;
        }
        void CreatTxtFileToDataBase(object obj, int fileState)
        {
            string logFile = obj.ToString();
            //BLLMail mail = new BLLMail();
            //mail.CreateTxtFile(int.Parse(this.TaskID), logFile, fileState);
            return;
        }

        /// <summary>  
        /// 按操作名记录异常信息，记入普通日志文件[区别于“按前缀记录新文件”]  
        /// </summary>  
        /// <param name="operatName">操作名称</param>  
        /// <param name="exp">错误信息</param>  
        public static void LogExp(string operatName, string exp)
        {
            ExceptionLog exceptionLog = new ExceptionLog();
            exceptionLog.OperatName = operatName;
            exceptionLog.DateCreated = DateTime.Now;
            exceptionLog.ExceptionMessage = exp;
            exceptionLog.LogToFile();
        }
        /// <summary>  
        /// 按前缀记录新文件[区别于 其他两种记录方式]  
        /// </summary>  
        /// <param name="preFix">新文件前缀</param>  
        /// <param name="exp">异常信息</param>  
        /// <param name="isNewFile">仅为区别与按操作名记录异常方式</param>  
        public static void LogExp(string preFix, string exp, bool isNewFile)
        {
            ExceptionLog exceptionLog = new ExceptionLog();
            exceptionLog.DateCreated = DateTime.Now;
            exceptionLog.ExceptionMessage = exp;
            exceptionLog.LogToFile(preFix);
        }
        /// <summary>  
        /// 普通日志记录  
        /// </summary>  
        /// <param name="exp">异常信息</param>  
        public static void LogExp(string exp)
        {
            ExceptionLog exceptionLog = new ExceptionLog();
            exceptionLog.DateCreated = DateTime.Now;
            exceptionLog.ExceptionMessage = exp;
            exceptionLog.LogToFile();
        }

        /// <summary>  
        /// 记录ID  
        /// </summary>  
        /// <param name="ids"></param>  
        public static void RecordIDs(string taskId, string ids, bool success)
        {
            ExceptionLog exceptionLog = new ExceptionLog();
            exceptionLog.TaskID = taskId;
            exceptionLog.IDS = ids;
            exceptionLog.IDsToFile(success);
        }

        /// <summary>  
        /// 记录不符合格式的邮件 返回文件路径  
        /// </summary>  
        /// <param name="taskId"></param>  
        /// <param name="email"></param>  
        /// <returns></returns>  
        public static string RecordFailEmail(string taskId, string email)
        {
            ExceptionLog exceptionLog = new ExceptionLog();
            exceptionLog.TaskID = taskId;
            exceptionLog.FailEail = email;
            return exceptionLog.FailEmailToFile();
        }


    }  
}
